package com.feng.chat.controller;

import com.feng.chat.dto.ContactListDTO;
import com.feng.chat.dto.Result;
import com.feng.chat.service.IGroupService;
import com.feng.chat.service.ITalkSessionService;
import com.feng.chat.utils.GetUserUtil;
import com.feng.chat.vo.NoticeEditVO;
import com.feng.chat.vo.TalkCreateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/v1/group")
public class GroupController {
    @Autowired
    private IGroupService groupService;



    /**
     * 获取邀请好友的好友列表
     * @param request
     * @param group_id
     * @return
     */
    @GetMapping("/member/invites")
    public Result invites(HttpServletRequest request,Integer group_id){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = groupService.invites(userId, group_id);
        return result;
    }

    /**
     * 创建群聊
     * @param request
     * @param map
     * @return
     */
    @PostMapping("/create")
    public Result create(HttpServletRequest request, @RequestBody Map<String,String> map){
        Integer userId = GetUserUtil.getUserId(request);
        String ids = map.get("ids");
        List<Integer> idList = Arrays.stream(ids.split(",")).map(Integer::valueOf).collect(Collectors.toList());
        String name = map.get("name");
        Result result = groupService.create(userId, idList, name);

        return result;
    }

    /**
     * 获取已加入的群聊列表
     * @param request
     * @return
     */
    @GetMapping("/list")
    public Result list(HttpServletRequest request){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = groupService.list(userId);
        return result;
    }

    /**
     * 获取群聊信息
     * @param request
     * @param group_id
     * @return
     */
    @GetMapping("/detail")
    public Result detail(HttpServletRequest request,Integer group_id){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = groupService.detail(userId, group_id);
        return result;
    }

    /**
     * 获取群聊成员
     * @param group_id
     * @return
     */
    @GetMapping("/member/list")
    public Result memberList(Integer group_id){
        Result result = groupService.memberList(group_id);
        return result;
    }

    /**
     * 根据群id获取群公告信息
     * @param group_id
     * @return
     */
    @GetMapping("/notice/list")
    public Result noticeList(Integer group_id){
        Result result = groupService.noticeList(group_id);
        return result;
    }

    /**
     * 设置用户群名片
     * @param request
     * @param map
     * @return
     */
    @PostMapping("/member/remark")
    public Result memberRemark(HttpServletRequest request,@RequestBody Map<String,String> map){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = groupService.memberRemark(userId, Integer.parseInt(map.get("group_id")), map.get("visit_card"));
        return result;
    }

    /**
     * 邀请好友进群
     * @param request
     * @param map
     * @return
     */
    @PostMapping("/invite")
    public Result invite(HttpServletRequest request,@RequestBody Map<String,String> map){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = groupService.invite(userId, Integer.parseInt(map.get("group_id")), map.get("ids"));
        return result;
    }

    /**
     * 退出群聊
     * @param request
     * @param map
     * @return
     */
    @PostMapping("/secede")
    public Result secede(HttpServletRequest request,@RequestBody Map<String,Integer> map){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = groupService.secede(userId, map.get("group_id"));
        return result;
    }

    /**
     * 添加群公告
     * @param request
     * @param noticeEditVO
     * @return
     */
    @PostMapping("/notice/edit")
    public Result noticeEdit(HttpServletRequest request, @RequestBody NoticeEditVO noticeEditVO){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = groupService.noticeEdit(userId, noticeEditVO);
        return result;
    }

    /**
     * 修改群信息
     * @param map
     * @return
     */
    @PostMapping("/setting")
    public Result setting(@RequestBody Map<String,String> map){
        Result result = groupService.setting(Integer.parseInt(map.get("group_id")), map.get("avatar"), map.get("group_name"), map.get("profile"));
        return result;
    }

    /**
     * 移除群成员
     * @param request
     * @param map
     * @return
     */
    @PostMapping("/member/remove")
    public Result memberRemove(HttpServletRequest request,@RequestBody Map<String,String> map){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = groupService.memberRemove(userId, Integer.parseInt(map.get("group_id")), map.get("members_ids"));
        return result;
    }

    /**
     * 解散群聊
     * @param request
     * @param group_id
     * @return
     */
    @PostMapping("/dismiss")
    public Result dismiss(HttpServletRequest request,@RequestBody String group_id){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = groupService.dismiss(userId, Integer.parseInt(group_id));
        return result;
    }
}
